package com.project.systemfront.controller;

import java.util.List;
import java.util.Map;

import com.project.systemfront.common.Result;
import com.project.systemfront.pojo.Likes;
import com.project.systemfront.service.IArtworkService;
import com.project.systemfront.service.ILikesService;
import com.project.systemfront.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 点赞Controller
 * 
 * @author ruoyi
 * @date 2025-01-23
 */
@RestController
@RequestMapping("/system/likes")
public class LikesController
{
    @Autowired
    private ILikesService likesService;

    @Autowired
    private IArtworkService artworkService;

    @GetMapping("/check/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        Map<String, Object> params = ThreadLocalUtil.get();
        Long userId = Long.parseLong(params.get("id").toString());
        Likes likes = new Likes();
        likes.setArtworkId(id);
        likes.setUserId(userId);
        List<Likes> list = likesService.selectLikesList(likes);
        if (list.size() > 0) {
            return Result.success("true");
        }else{
            return Result.success("false");
        }
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        Map<String, Object> params = ThreadLocalUtil.get();
        Long userId = Long.parseLong(params.get("id").toString());
        Likes likes = new Likes();
        likes.setArtworkId(id);
        likes.setUserId(userId);
        List<Likes> list = likesService.selectLikesList(likes);
        if (list.size() > 0) {
            likesService.deleteLikesById(list.get(0).getId());
            artworkService.decrementLikeCount(id);
            return Result.success("取消点赞成功");
        }else{
            return Result.error(500,"取消点赞失败");
        }
    }



  }
